package cn.hotel.controller.worker;

import cn.hotel.entity.RoomTypeMoney;
import cn.hotel.entity.Worker;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.RoomTypeMoneyService;
import cn.hotel.service.WorkerService;
import cn.hotel.utils.EMS;
import cn.hotel.utils.MD5Utils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping(value = "/admin/operator")
public class OperatorController {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private RoomTypeMoneyService moneyService;

//    @PostMapping()
//    public Result getAllWorkers(){
//        List<Worker> workers = workerService.queryAllWorkers();
//        return ResponseTool.success(workers);
//    }

    /**
     * 分页查询操作员管理信息
     * @return
     */
    @PostMapping("/list")
    public Result getAllWorkers(Integer page , Integer pageSize){
        PageInfo<Worker> workerPageInfo = workerService.queryWorkerList(page, pageSize);
        return ResponseTool.success(workerPageInfo);
    }

    /**
     * 发送短信验证码
     */
    @PostMapping("/sendCode")
    public Result sendCode(String phone) throws Exception {
        log.info("phone：{}",phone);
        //随机生成六位验证码
        int sum = 0;
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            sum = sum*10 + (random.nextInt(9)+1);
        }
        System.out.println(sum);
        //开始发送验证码
        EMS.getCheckWord(phone,sum);
        return ResponseTool.success(sum);
    }

    /**
     * 新增员工信息
     */
    @PostMapping("/add")
    public Result addWorker(Worker worker){
        String password = worker.getPassword();
        worker.setPassword(MD5Utils.MD5Encode(password));
        log.info("worker:{}",worker);
        if(worker !=null){
            workerService.insertWorkerInfo(worker);
            return ResponseTool.success("新增员工成功");
        }else {
            return ResponseTool.failed("新增员工失败");
        }
    }

    /**
     * 通过员工id获取员工信息
     */
    @PostMapping("/{workerId}")
    public Result getWorkerInfoById(@PathVariable("workerId") int workerId){
        log.info("workerId:{}",workerId);
        Worker worker = workerService.queryWorkerInfo(workerId);
        if(worker != null){
            return ResponseTool.success(worker);
        }else {
            return ResponseTool.failed("编辑查询失败");
        }
    }

    /**
     * 更新员工信息
     * @param worker
     * @return
     */
//    @PostMapping("/update")
//    public Result changeWorkerInfo(Worker worker){
//        log.info("worker:{}",worker);
//        if(worker != null){
//            workerService.updateWorkerInfo(worker);
//            return ResponseTool.success("员工信息编辑成功");
//        }else {
//            return ResponseTool.failed("员工信息编辑失败");
//        }
//    }

    @PostMapping("/update")
    public Result aaa(Worker worker){
        log.info("获取的worker的信息{}" , worker);
        int i = workerService.updateWorker(worker);
        if (i == 0) {
            return ResponseTool.failed("更新失败");
        }
        return ResponseTool.success("更新失败");
    }

    /**
     * 删除员工信息
     */
    @DeleteMapping("/delete")
    public Result removeWorkerInfo(@RequestParam("workerIds") List<Integer> workIds){
        log.info("workIds:{}",workIds);
        for (Integer workId : workIds) {
            workerService.deleteWorkerInfo(workId);
        }
        return ResponseTool.success("删除员工信息成功");
    }

    /**
     * 查看房间类型的收入情况
     */
    @PostMapping("/money")
    public Result getRoomTypeMoney(){
        List<RoomTypeMoney> roomTypeMonies = moneyService.queryRoomTypeMoney();
        log.info("roomTypeMonies:{}",roomTypeMonies);
        return ResponseTool.success(roomTypeMonies);
    }
}
